package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.Pay;
import com.armani.model.Result;
import com.armani.service.PayService;
import com.sun.tracing.dtrace.Attributes;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: PayController
 * @Description: TODO
 * @Author: Roc
 * @Date: 2021/11/28 20:15
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @GetMapping(value = "/list/search.do")
    @ResponseBody
    public String ListSearch(@RequestParam(required = false) Integer userId,
                             @RequestParam(required = false) Integer state,
                             @RequestParam(required = false) String startTime,
                             @RequestParam(required = false) String endTime ,
                             @RequestParam(required = false) Integer page,@RequestParam Integer limit) {

        Integer startRow = (page - 1) * limit;

        List<Pay> pays = payService.ListSearch(userId,state,startTime,endTime,startRow,limit);
        Integer count =payService.getCount(userId,state,startTime,endTime);

        Result result = new Result();
        result.setData(pays);
        result.setCount(count);
        result.setCode(0);

        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/insert.do")
    @ResponseBody
    public String insert(@RequestParam(required = false) Integer userId,
                         @RequestParam(required = false) Integer orderId,
                         @RequestParam(required = false) String payTime,
                         @RequestParam(required = false) BigDecimal payment,
                         @RequestParam(required = false) Integer payway,
                         @RequestParam(required = false) Integer state) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date payTimes = sdf.parse(payTime);

        payService.insert( userId,  orderId,  payTimes, payment, payway, state);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/update.do")
    @ResponseBody
    public String update(@RequestParam Integer userId,
                         @RequestParam Integer orderId,
                         @RequestParam String payTime,
                         @RequestParam BigDecimal payment,
                         @RequestParam Integer payway,
                         @RequestParam Integer state,
                         @RequestParam Integer id) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date payTimes = sdf.parse(payTime);

        payService.update( userId,  orderId,  payTimes, payment, payway, state,id);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        payService.delete(id);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }
}
